package com.qf.merchant.controller;


import com.qf.common.http.Result;
import com.qf.common.http.ResultCode;
import com.qf.service.impl.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class AdminController {

    @Resource
    private AdminService  adminService;

    @RequestMapping("login")
    public String toLoginAll(){
        return "login";
    }

   @RequestMapping("login.do")
   @ResponseBody
    public Result<String> toLogin(@RequestParam(value = "username" , required = true) String username ,
                          @RequestParam(value = "password" , required = true) String password){

        //返回什么类型就是什么类型  因为在实际项目中也不知道到底返回的什么类型
       UsernamePasswordToken token = new UsernamePasswordToken(username, password);
       Subject subject = SecurityUtils.getSubject();
       try {
           subject.login(token);
           if(subject.isAuthenticated()){
               return Result.create(ResultCode.LOGIN_SUCCESS);
           }
       } catch (AuthenticationException e) {
       }
       return Result.create(ResultCode.LOGIN_ERROR);
   /* @RequestMapping("login.do")
    @ResponseBody
    public Result<String> doLogin(@RequestParam(value = "username", required = true) String username,
                                  @RequestParam(value = "password", required = true) String password) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            if(subject.isAuthenticated()){
                return Result.create(ResultCode.LOGIN_SUCCESS);
            }
        } catch (AuthenticationException e) {
        }
        return Result.create(ResultCode.LOGIN_ERROR);*/
//        Admin01 admin = adminService.getByUserName(username);
//        if (admin == null) {
//            return Result.create(ResultCode.LOGIN_ERROR);
//        }
//        if (!admin.getPassword().equals(password)) {
//            return Result.create(ResultCode.LOGIN_ERROR);
//        }
//        return Result.create(ResultCode.LOGIN_SUCCESS);

}}
